package com.二叉树2;


import java.util.*;

/**
 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
 *
 * For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
 *
 *     1
 *    / \
 *   2   2
 *  / \ / \
 * 3  4 4  3
 *
 *
 * But the following [1,2,2,null,3,null,3] is not:
 *
 *     1
 *    / \
 *   2   2
 *    \   \
 *    3    3
 *
 *
 * Follow up: Solve it both recursively and iteratively.
 */
public class 对称树 {
    static class Solution {
        /**
         * 使用队列每次比较左右两个值
         * @param root
         * @return
         */
        public boolean isSymmetric(TreeNode root) {
            if(root == null){
                return  true;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            queue.add(root);
            while (!queue.isEmpty()){
                TreeNode left = queue.poll();
                TreeNode right = queue.poll();
                if(left == null && right == null){
                    continue;
                }
                if(left == null || right == null){
                    return  false;
                }
                if(left.val != right.val){
                    return false;
                }
                queue.add(left.left);
                queue.add(right.right);
                queue.add(left.right);
                queue.add(right.left);
            }
            return true;
        }


    }


        public static void main(String[] args) {
            Solution solution = new Solution();
            boolean flag = solution.isSymmetric(new TreeNode(new int[]{1,0}));
        }
    }

